#include "BgmListManager.h"
#include "Protocol/ProtocolApi.h"
#include "unzip.h"
#include "StreamerCurtainPlay.h"
#include "log.h"
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include "Network/Socket.h"
#include <string>
#include <vector>
#include "DiskController.h"
#include "StreamerDownloadOnlineFile.h"

#include "ConfigApi.h"





extern "C" int getBgmCurtainState(int* state)
{
    //printf("enter getBgmCurtainState\n");
    if(BgmListManager::Instance()->isBgmCurtainExist())
    {
        *state = 0;
    }
    else
    {
        *state = -1;
    }
    return 0;
}

using namespace Protocol;
using namespace ConfigApi;



BgmListManager::Ptr BgmListManager::Instance(){
    static auto ptr = std::make_shared<BgmListManager>();
    return ptr;
}


BgmListManager::BgmListManager(){
    RegistRPC();
}

BgmListManager::~BgmListManager(){

}

void BgmListManager::RegistRPC(){
    IProtocolInterface::GetInstance()->AuditoryworksRegistRequestRpc("bgmList", 
        [this](const Json::Value& req_params, Json::Value& res_result, std::string& errinfo){
            return bgmList(req_params, res_result, errinfo);
        });

    IProtocolInterface::GetInstance()->AuditoryworksRegistRequestRpc("closeBgm", 
        [this](const Json::Value& req_params, Json::Value& res_result, std::string& errinfo){
            return clearBgmList(req_params, res_result, errinfo);
        });
}



bool BgmListManager::isBgmCurtainExist()
{
    std::string path;
	Json::Value nameTable;
	IConfigInterface::GetInstance()->Get("BgmInfo", nameTable);
	if(nameTable.isMember("BgmPath") && nameTable["BgmPath"].isString())
	{
		path = nameTable["BgmPath"].asString();

        if(path.size() > 0)
        {
            //printf("isBgmCurtainExist path=%s\n", path.c_str());
            return access(path.c_str(), F_OK) == 0;
        }		
	}

    return false;
}

bool BgmListManager::clearBgmList(const Json::Value& req_params, Json::Value& res_result, std::string& errinfo)
{
    //停止当前bgm播放
    Media::BgmCurtainPlay::instance()->stopBgmCurtain();
    
    //清除bgm的路径信息
    Json::Value bgmTable;
    bgmTable["BgmPath"] = "";
    IConfigInterface::GetInstance()->Set("BgmInfo", bgmTable, ConfigOptions::ConfigOptionPart);

    //清除播放文件
    if(!DiskController::instance()->setRealTimeRootPath()){
        LOG_PrintError("faild get root path\n");
        return false;
    }
    
    return DiskController::instance()->clearStreamerFiles(BGM_ROOT_DIR); //清除上次播放信息
}


bool BgmListManager::bgmList(const Json::Value& req_params, Json::Value& res_result, std::string& errinfo){
    Json::Value bgmTable = req_params["bgmlist"];
    if (!bgmTable.isArray()){
        return false;
    }

    if(!DiskController::instance()->setRealTimeRootPath()){
        LOG_PrintError("faild get root path\n");
        return false;
    }
    DiskController::instance()->clearStreamerFiles(BGM_ROOT_DIR); //清除上次播放信息
    
    std::string path;
    DiskController::instance()->getRealTimeRootPath(path);

    //更新bgm的播放信息
    string despPath = path + BGM_ROOT_DIR + BGM_DESCRIBE;
	DiskController::instance()->createFileIfNotExists(despPath);
    ofstream ofs;
	ofs.open(despPath);
    if(!ofs.is_open()) {
		LOG_PrintError("faild to open ofstream\n");
		return false;
    }
	ofs << req_params.toStyledString();
	ofs.close();


    //保存bgm播放信息的路径信息
    Json::Value configTable;
    configTable["BgmPath"] = path + BGM_ROOT_DIR;
    IConfigInterface::GetInstance()->Set("BgmInfo", configTable, ConfigOptions::ConfigOptionPart);


    Media::BgmCurtainPlay::instance()->playBgmCurtain(path);

    return true;
}